<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>平方练习</title>
</head>
<body>
<input type="text">
<input type="button" value="平方" onclick="f1()">
<input type="button" value="相加" onclick="f2()">
<h1>结果:<span></span></h1>
<script>
    let i = document.querySelector("input");
    let s = document.querySelector("span");
    function f1() {
        //Not a Number:不是一个数 当NaN和任何数值进行任何运算时结果都为NaN
        if (isNaN(i.value)){
            s.innerText="输入错误";
            return;
        }
        s.innerText = i.value*i.value;
    }
    function f2() {
        if (isNaN(i.value)){
            s.innerText="输入错误";
            return;
        }
        //当字符串进行-*/运算时会自动转成数值, 加法运算是拼接
        // s.innerText = parseFloat(i.value)+parseFloat(i.value);
        s.innerText = i.value*1+i.value*1;

    }
</script>
</body>
</html>